//
//  SurroundingViewTVController.m
//  WildDonkey
//
//  Created by 佟小波 on 15/3/30.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "SurroundingViewTVController.h"
#import "SurDetailTVCell.h"
#import "AFNetworking.h"
#import "SurFoodDtl.h"
#import "UIImageView+WebCache.h"

@interface SurroundingViewTVController ()

@property (nonatomic,retain) NSMutableArray *surfoodArr;

@end

@implementation SurroundingViewTVController

- (void)dealloc
{
    [_surfoodArr release];
    [super dealloc];
}


- (void)loadData
{
    
    
    NSString *url=[NSString stringWithFormat:@"http://www.roadqu.com/api/mobile/qunawan/poi/search&a_t=1427972317000&token=55150e044f7c3&pageflag=nearby&eachpage=20&radius=0&lng=%d",self.viewPoint.lng];
    
    NSString *url1=[[NSString stringWithFormat:@"&lat=%d",self.viewPoint.lat] stringByAppendingString:@"&startpos=0&sign=b3550d96aa51668d7892e1a758fe23164ac97e3e"];
    
    NSString *url2=[url stringByAppendingString:url1];
    
    
    
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    [mgr GET:url2 parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //        NSLog(@"-----++++++%@",responseObject);
        
        
        NSArray *data = [responseObject objectForKey:@"data"];
        self.surfoodArr = [NSMutableArray arrayWithCapacity:[data count]];
        
        for (NSDictionary *event in data) {
            SurFoodDtl *foodDtl=[[SurFoodDtl alloc]init];
            [foodDtl setValuesForKeysWithDictionary:event];
            
            [self.surfoodArr addObject:foodDtl];
            [foodDtl release];
        }
        SurFoodDtl *t = _surfoodArr[0];
        //        NSLog(@" 44%@ 44%@",t.name,t.shortdesc);
        
        [self.tableView reloadData];//相当于刷新
        
    } failure:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor clearColor];
    self.navigationItem.title=@"周边景点";
    
    UIImageView *bgView = [[UIImageView alloc] init] ;
    bgView.image = [UIImage imageNamed:@"background"];
    self.tableView.backgroundView = bgView;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self loadData];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_surfoodArr count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SurFoodDtl *sd=_surfoodArr[indexPath.row];
    
    static NSString *identifier = @"food";
    SurDetailTVCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell=[[SurDetailTVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell.imgView setImageWithURL:[NSURL URLWithString:sd.avatar]];
    cell.nameLabel.text=sd.name;
    cell.shortdescLabel.text=sd.shortdesc;
    
    cell.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor=[UIColor yellowColor];
    view.alpha=0.7;
    
    
    UIImageView *imgView=[[UIImageView alloc]init];
    imgView.frame=CGRectMake(5, 5, 25, 25);
    imgView.image=[UIImage imageNamed:@"poi"];
    [view addSubview:imgView];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(35, 5, 300, 25)];
    label.text=self.viewPoint.name;
    label.font=[UIFont systemFontOfSize:16];
    [view addSubview:label];
    
    return [view autorelease];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
